package com.leyou.item.web;


import com.leyou.common.annotation.EnableExceptionAnnotation;
import com.leyou.common.dto.*;
import com.leyou.item.entity.Sku;
import com.leyou.item.entity.Spu;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpecParamService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("goods")
@EnableExceptionAnnotation
public class GoodsController {

    @Autowired
    private SpuService spuService;

    @Autowired
    private SkuService skuService;

    @Autowired
    private SpuDetailService spuDetailService;

    @Autowired
    private SpecParamService specParamService;

    /**
     *  分页查询
     * @param brandId 品牌id
     * @param categoryId 目录id
     * @param id 商品id
     * @param page 当前页
     * @param rows 每页的行数
     * @param saleable 是否上架
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> querySpuByPage(
            @RequestParam(value = "brandId", required = false) Long brandId,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable", required = false) Boolean saleable
    ) {

        return ResponseEntity.ok(spuService.queryByPage(brandId, categoryId, id, page, rows, saleable));
    }

    /**
     * 新增商品
     *
     * @param spuDTO
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO) {
        spuService.saveGoods(spuDTO);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 商品上下架
     *
     * @param id       spuid
     * @param saleable 上下架
     * @return 无
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSaleable(@RequestParam("id") Long id, @RequestParam("saleable") Boolean saleable) {
        spuService.updateSaleable(id, saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据id查询spu商品信息
     *
     * @param id spu id
     * @return 商品信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryGoodsById(@PathVariable("id") Long id) {
        SpuDTO spuDTO = spuService.querySpuAndDetailAndSku(id);

        return ResponseEntity.ok(spuDTO);
    }

    /**
     * 根据id修改商品信息
     *
     * @param spuDTO
     * @return 无
     */
    @PutMapping
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO) {
        spuService.updateGoods(spuDTO);

        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据商品id查询spulist
     * @param ids spu ids
     * @return 无
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuByIds(@RequestParam("ids") List<Long> ids) {

        return ResponseEntity.ok(SkuDTO.convertEntityList(skuService.listByIds(ids)));

    }

    /**
     * 根据spuid查询spu集合
     * @param id spuid
     * @return
     */

    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id") Long id) {

        List<Sku> skuList = skuService.query().eq("spu_id", id).list();

        return ResponseEntity.ok(SkuDTO.convertEntityList(skuList));

    }

    /**
     * 根据spuid查询商品的详细信息
     * @param id spuid
     * @return
     */

    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(@RequestParam("id") Long id) {
        SpuDetail spuDetail = spuDetailService.getById(id);

        return ResponseEntity.ok(new SpuDetailDTO(spuDetail));
    }

    /**
     * 根据spuId查询spu的所有规格参数值
     * @param id spuid
     * @param searching 是否为查询条件
     * @return
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecsValues(
            @RequestParam("id") Long id,
            @RequestParam(value = "searching", required = false) Boolean searching) {

        List<SpecParamDTO> specParamDTOList = spuService.querySpuDetailById(id, searching);

        return ResponseEntity.ok(specParamDTOList);
    }

    /**
     *  根据商品id查询商品详细信息
     * @param  id 商品id
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long id){
        Spu spu = spuService.getById(id);

        return ResponseEntity.ok(new SpuDTO(spu));
    }

    /**
     * 扣减库存
     * @param cartMap 商品集合
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> deductStock(@RequestBody Map<Long, Integer> cartMap){
        skuService.deductStock(cartMap);
        return ResponseEntity.noContent().build();
    }
}